package com.qf.smartplatform.interceptors;

import com.qf.smartplatform.pojo.Menu;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.qf.smartplatform.cache.MenuCache;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * projectName:day12_27_Test
 * author:guoLei
 * time:2022/1/9 18:02
 * description:
 */
@Component
public class ApiCheckInter implements HandlerInterceptor {
    private MenuCache menuCache;

    @Autowired
    public void setMenuCache(MenuCache menuCache) {
        this.menuCache = menuCache;
    }

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String requestURI = request.getRequestURI();
        System.err.println("请求地址" + requestURI);
        Menu menuByUrl = menuCache.getMenuByUrl(requestURI);
        if (menuByUrl != null) {
            String enable = menuByUrl.getEnable();
            if (!"1".equals(enable)){
                response.getWriter().write("已拦截");
            }
            return "1".equals(enable);
        }
        return true;
    }
}
